package com.unlcn.ils.erpInterfaces.api.controller;

import com.alibaba.fastjson.JSON;
import com.unlcn.ils.erpInterfaces.api.vo.RestVO;
import com.unlcn.ils.erpInterfaces.api.vo.ShippingInfoVO;
import com.unlcn.ils.erpInterfaces.dao.model.LisaRequestLog;
import com.unlcn.ils.erpInterfaces.dao.model.bo.VehicleQueueBO;
import com.unlcn.ils.erpInterfaces.service.buss.ILisaRequestLogService;
import com.unlcn.ils.erpInterfaces.service.buss.ISmvehiclequeueService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

@RestController
@RequestMapping(value = "/queue", produces = MediaType.APPLICATION_JSON_VALUE)
@Api(value = "/queue", description = "报班接口",tags={"报班接口"})
public class VehicleQueueController {

    private static final Logger LOGGER = LoggerFactory.getLogger(VehicleQueueController.class);

    @Autowired
    private ISmvehiclequeueService smvehiclequeueService;

    @Autowired
    private ILisaRequestLogService lisaRequestLogService;

    @PostMapping(value = "/vehicleQueue")
    @ApiOperation(value = "报班", notes = "报班", produces = MediaType.APPLICATION_JSON_VALUE,response = RestVO.class)
    public RestVO<ShippingInfoVO> vehicleQueue(@RequestBody VehicleQueueBO vehicleQueueBO){

        RestVO<ShippingInfoVO> restVO = null;

        LisaRequestLog lisaRequestLog = new LisaRequestLog();
        lisaRequestLog.setWay("POST");
        lisaRequestLog.setUrl("/queue/vehicleQueue");
        lisaRequestLog.setData(JSON.toJSONString(vehicleQueueBO));
        lisaRequestLog.setDriverQueueId(vehicleQueueBO.getDriverQueueId());
        lisaRequestLog.setGmtCreate(new Date());

        try {
            Long erpQueueId = smvehiclequeueService.vehicleQueue(vehicleQueueBO);
            restVO =  new RestVO(0, "success", true);
            lisaRequestLog.setIsSuccess(1);
            lisaRequestLog.setFlag(1);
            lisaRequestLog.setReturnData(JSON.toJSONString(restVO));
            lisaRequestLog.setErpQueueId(erpQueueId);
        } catch (Exception e) {
            LOGGER.error("VehicleQueueController.vehicleQueue ERROR Message : {} ", e);
            restVO = new RestVO<>(-1, e.getMessage(), false);
            lisaRequestLog.setIsSuccess(0);
            lisaRequestLog.setFlag(0);
            lisaRequestLog.setReturnData(JSON.toJSONString(restVO));
        }

        // 插入日志
        lisaRequestLog.setIlineid(lisaRequestLogService.getNextIlineid());
        lisaRequestLogService.insertLog(lisaRequestLog);

        return restVO;
    }

    @PostMapping(value = "/cancelVehicleQueue")
    @ApiOperation(value = "取消报班", notes = "取消报班", produces = MediaType.APPLICATION_JSON_VALUE,response = RestVO.class)
    public RestVO<Boolean> cancleVehicleQueue(@RequestBody VehicleQueueBO vehicleQueueBO){
        RestVO<Boolean> restVO = null;

        Long driverQueueId = vehicleQueueBO.getDriverQueueId();

        LisaRequestLog lisaRequestLog = new LisaRequestLog();
        lisaRequestLog.setWay("POST");
        lisaRequestLog.setUrl("/queue/cancelVehicleQueue");
        lisaRequestLog.setData(driverQueueId.toString());
        lisaRequestLog.setDriverQueueId(driverQueueId);
        lisaRequestLog.setGmtCreate(new Date());

        try {
            Boolean blnRtn = smvehiclequeueService.cancelVehicleQueue(driverQueueId);
            restVO =  new RestVO(0, "success", true);
            restVO.setData(blnRtn);
            lisaRequestLog.setIsSuccess(1);
            lisaRequestLog.setFlag(1);
            lisaRequestLog.setReturnData(JSON.toJSONString(restVO));
        } catch (Exception e) {
            LOGGER.error("VehicleQueueController.cancleVehicleQueue ERROR Message : {} ", e);
            restVO = new RestVO<>(-1, e.getMessage(), false);
            restVO.setData(false);
            lisaRequestLog.setIsSuccess(0);
            lisaRequestLog.setFlag(0);
            lisaRequestLog.setReturnData(JSON.toJSONString(restVO));
        }

        // 插入日志
        lisaRequestLog.setIlineid(lisaRequestLogService.getNextIlineid());
        lisaRequestLogService.insertLog(lisaRequestLog);

        return restVO;
    }

}
